package com.serkol.ams.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.serkol.ams.bean.MyPageOld;
import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.Coupon;
import com.serkol.ams.service.CouponService;
import com.serkol.constant.ResultConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/coupon")
public class CouponApiController {


    @Autowired
    private CouponService couponService;


    @GetMapping("/findByList")
    @ResponseBody
    public Result findByList() {
        QueryWrapper<Coupon> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().orderByDesc(Coupon::getCreateDate);
        List<Coupon> list = couponService.list(queryWrapper);
        return Result.ok(list);
    }

    @GetMapping("/findByPageList")
    public Result findByPageList(@RequestParam(name = "pageSize", defaultValue = "10") int pageSize, @RequestParam(name = "current", defaultValue = "0") int current) {

        Result result = new Result();
        MyPageOld list = couponService.findByPageList(pageSize, current);
        if (list.getSize() > 0) {
            result.setStatus(ResultConstant.SUCSESS);
            result.setData(list);
        } else {
            result.setStatus(ResultConstant.EMPTIY);
        }
        return result;
    }

    //findById
    @GetMapping("/findById")
    @ResponseBody
    public Result findById(@RequestParam(value = "id", defaultValue = "0")  long id) {
        Coupon byId = couponService.getById(id);
        return Result.ok(byId);
    }
}
